package com.example.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.entity.Book;
import com.example.demo.mapper.BookMapper;
import com.example.demo.service.BookService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class BookServiceImpl implements BookService {

    @Resource
    private BookMapper bookMapper;

    @Override
    public boolean save(Book book) {
        return bookMapper.insert(book) > 0;
    }

    @Override
    public boolean update(Book book) {
        return bookMapper.updateById(book) > 0;
    }

    @Override
    public boolean deleteBatch(List<Integer> ids) {
        return bookMapper.deleteBatchIds(ids) >= 0;
    }

    @Override
    public boolean deleteById(Long id) {
        return bookMapper.deleteById(id) > 0;
    }

    @Override
    public Page<Book> findPage(Integer pageNum, Integer pageSize, String search) {
        LambdaQueryWrapper<Book> wrapper = Wrappers.<Book>lambdaQuery();
        if (StringUtils.isNotBlank(search)) {
            // 按书名或作者等进行模糊搜索
            wrapper.like(Book::getName, search)
                   .or()
                   .like(Book::getAuthor, search)
                   .or()
                   .like(Book::getIsbn, search);
        }
        return bookMapper.selectPage(new Page<>(pageNum, pageSize), wrapper);
    }
    
    @Override
    public Page<Book> advancedSearch(Integer pageNum, Integer pageSize, 
                                   String isbnSearch, String nameSearch, String authorSearch) {
        LambdaQueryWrapper<Book> wrapper = Wrappers.<Book>lambdaQuery();
        
        if (StringUtils.isNotBlank(isbnSearch)) {
            wrapper.like(Book::getIsbn, isbnSearch);
        }
        
        if (StringUtils.isNotBlank(nameSearch)) {
            wrapper.like(Book::getName, nameSearch);
        }
        
        if (StringUtils.isNotBlank(authorSearch)) {
            wrapper.like(Book::getAuthor, authorSearch);
        }
        
        return bookMapper.selectPage(new Page<>(pageNum, pageSize), wrapper);
    }
} 